package com.breaktian.network.api.finance;

import android.support.annotation.NonNull;

import com.alibaba.fastjson.TypeReference;
import com.breaktian.network.api.base.BaseApi;
import com.breaktian.network.http.Http;
import com.breaktian.network.http.HttpException;
import com.breaktian.network.http.RequestCallback;

import java.util.Map;

/**
 * Created by keke.tian on 2018/4/3.
 */

public class BaseFinaceApi extends BaseApi{
    public <T> ApiResult<T> request(@NonNull Http http, @NonNull TypeReference<ApiResult<T>> reference,final ApiCallback<T> callback){
        ApiResult<T> cacheResult = http.enqueue(reference, new RequestCallback<ApiResult<T>>() {
            @Override
            public void onStart(Http http) {
                //添加公共参数
                http.param("apiKey", "").param("reqTime", String.valueOf(System.currentTimeMillis() / 1000))
                        .param("device", "android").param("app_name", "vffinance").param("app_version", "1.0").asDefault();
//                        .header("Authorization", "OAuth sign=" + NativeSign.makeSign(sContext, getVerifySecret(), http.getParams()));//验签
            }

            @Override
            public void onSuccess(ApiResult<T> apiResult, Map<String, String> hraders) {
                callback.onResult(apiResult);
                if(apiResult.isSuccess()){
                    callback.onSuccess(apiResult.getResults());
                }else{
                    // TODO 业务逻辑错误码
                    callback.onFail(new ApiResult.ApiError(-2,"业务逻辑错误码"));
                }
            }

            @Override
            public void onFailure(HttpException e) {
                callback.onFail(new ApiResult.ApiError(-1,"网络异常"));
            }
        });

        return cacheResult;
    }
}
